import { forEachValue } from '../util';

export default class Module {
  get namespaced() {
    return !!this._raw.namespaced;
  }

  constructor(newModule) {
    this._raw = newModule;
    this._children = {};
    this.state = newModule.state;
  }

  getChild(key) {
    return this._children[key];
  }

  addChild(key, module) {
    this._children[key] = module;
  }

  // 给模块继续扩展方法
  forEachGetters(fn) {
    const { getters } = this._raw;
    if (getters) {
      forEachValue(getters, fn);
    }
  }

  // 给模块继续扩展方法
  forEachMutations(fn) {
    const { mutations } = this._raw;
    if (mutations) {
      forEachValue(mutations, fn);
    }
  }

  // 给模块继续扩展方法
  forEachActions(fn) {
    const { actions } = this._raw;
    if (actions) {
      forEachValue(actions, fn);
    }
  }

  // 给模块继续扩展方法
  forEachChild(fn) {
    forEachValue(this._children, fn);
  }
}
